package com.model
{
	import com.manager.CarXmlManager;
	import com.vo.CarVo;
	import com.vo.CarportVo;
	
	import flash.geom.Vector3D;
	import flash.utils.Dictionary;

	public class CarDataBase
	{
		private static var carDic:Dictionary;
		private static var _carNameVo:CarportVo;
		private static var _carNameList:Array =[];
		private static var _chevroletCamaroList:Array =[];
		private static var _ferrariCaliforniaList:Array =[];
		private static var _mercedesMCLarenSLR500List:Array =[];
		private static var _mitsubishiLancerXList:Array =[];
		private static var _nissan350zList:Array =[];
		private static var _astonMartinVintageList:Array =[];
		public static function get carNameList():Array{
			return _carNameList;
		}
		public static function get chevroletCamaroList():Array{
			return _chevroletCamaroList;
		}
		public static function get ferrariCaliforniaList():Array{
			return _ferrariCaliforniaList;
		}
		public static function get mercedesMCLarenSLR500List():Array{
			return _mercedesMCLarenSLR500List;
		}
		public static function get mitsubishiLancerXList():Array{
			return _mitsubishiLancerXList;
		}
		public static function get nissan350zList():Array{
			return _nissan350zList;
		}
		public static function get astonMartinVintageList():Array{
			return _astonMartinVintageList;
		}

		public static function createCarDic():void{
			if(carDic == null) carDic = new Dictionary(true);
			
			var carList:XMLList = CarXmlManager.carList;
			var lengthA:int = carList.Audi.length();
			var lengthB:int = carList.ChevroletCamar.length();
			garageData(carList.Audi,_carNameList);
			garageData(carList.ChevroletCamar,_chevroletCamaroList);
			garageData(carList.FerrariCalifornia,_ferrariCaliforniaList);
			garageData(carList.MercedesMCLarenSLR500,_mercedesMCLarenSLR500List);
			garageData(carList.MitsubishiLancerX,_mitsubishiLancerXList);
			garageData(carList.Nissan350z,_nissan350zList);
			garageData(carList.AstonMartinVintage,_astonMartinVintageList);
		}
		
		public static function garageData(carList:XMLList,list:Array):void{
			var x:Number = 0;
			var y:Number = 0;
			var z:Number = 0;
			for(var i:int = 0;i < carList.length();i++){
				var _carNameVo:CarportVo= new CarportVo();
				_carNameVo.name = carList[i].name;
				_carNameVo.model = carList[i].model;
				_carNameVo.rootPath = carList[i].rootPath;
				list.push(_carNameVo);
				//左前轮坐标
				x = Number(carList[i].leftForewheel.@x);
				y = Number(carList[i].leftForewheel.@y);
				z = Number(carList[i].leftForewheel.@z);
				var leftForewheelPoint3D:Vector3D = new Vector3D(x,y,z);
				//右前轮坐标
				x = Number(carList[i].rightForewheel.@x);
				y = Number(carList[i].rightForewheel.@y);
				z = Number(carList[i].rightForewheel.@z);
				var rightForewheelPoint3D:Vector3D = new Vector3D(x,y,z);
				//左后轮坐标
				x = Number(carList[i].leftRearWheel.@x);
				y = Number(carList[i].leftRearWheel.@y);
				z = Number(carList[i].leftRearWheel.@z);
				var leftRearWheelPoint3D:Vector3D = new Vector3D(x,y,z);
				//右前轮坐标
				x = Number(carList[i].rightRearWheel.@x);
				y = Number(carList[i].rightRearWheel.@y);
				z = Number(carList[i].rightRearWheel.@z);
				var rightRearWheelPoint3D:Vector3D = new Vector3D(x,y,z);
				
				carDic[_carNameVo.model] = {rootPath:_carNameVo.rootPath,
					leftForewheel:leftForewheelPoint3D,
					rightForewheel:rightForewheelPoint3D,
					leftRearWheel:leftRearWheelPoint3D,
					rightRearWheel:rightRearWheelPoint3D};
			}
		}
		public static function getCarVo(carModel:String):CarVo{
			if(carDic == null)createCarDic();
			var cObj:Object = carDic[carModel];
			var path:String = cObj.rootPath;
			var cvo:CarVo = new CarVo();
			cvo.leftForewheel = cObj.leftForewheel;
			cvo.rightForewheel = cObj.rightForewheel;
			cvo.leftRearWheel = cObj.leftRearWheel;
			cvo.rightRearWheel = cObj.rightRearWheel;
			cvo.bodyPath = path + "body.xml";
			cvo.glassPath = path + "glass.xml";
			cvo.interiorPath = path + "interior.xml";
			cvo.bumperPath = path + "bumper.xml";
			cvo.interiorMap = path + "InteriorSkin.jpg";
			cvo.shadowMap = path + "CarShadow.png";
			cvo.bodyObjPath = path + "body.xml";
			cvo.wheelObjPath = path + "wheel.xml";
			return cvo;
		}
		
		
	}
}